/*
 * This class is for managing ads. This class gets all the image files from a
 * given directory and shows them on a jlabel. The default path to image is
 * "banners" direcotry which is relevant to the executable jar. this variable 
 * can be changed with by overriding 
 * the default parameter. Also the number of random ads could to be provided as 
 * a second parameter
 */
package pos.ads;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Random;

/**
 *
 * @author Parvez
 */
public class AdsManager {
    private ArrayList<Advertisement> adsPool = new ArrayList<Advertisement>();
    private int randomValue;
    private ArrayList<Integer> adsArr;
    private int totalAds;
    private String adsHomeDirectory;
    
    /**
     * default constructor will set the default path
     */
    public AdsManager() {
        this.adsHomeDirectory = "banners";
        this.analyseAdsPool();
    }
    
    /**
     * parameterised constructor will set the given path
     */
    public AdsManager(String path) {
        this.setAdsHomeDirectory(path);
        this.analyseAdsPool();
    }
    
    /**
     * parameterised constructor will set the given path and size of the random
     * ads array size
     */
    public AdsManager(String path, int numOfAds) {
        this.setAdsArrSize(numOfAds);
        this.adsHomeDirectory = path;
        this.analyseAdsPool();
        this.randomiseAdsArr();
    }
    
    /**
     * set unique random values to the adsarr variable
     */
    private void randomiseAdsArr() {
//        System.out.println(this.getAdsArr().size());
        for(int i = 0; i < this.getAdsArr().size(); i++) {
//            System.out.println(this.totalAds >= this.getAdsArr().size());
            if(this.totalAds >= this.getAdsArr().size()) {
                while(true) {
                    if(!this.adsArr.contains(this.getRandomValue())) {
                        this.randomValueGenerator();
                    }
                    else {
                        this.setAdsArr(i, this.getRandomValue());
                        break;
                    }
                }
//                System.out.println(this.getRandomValue());
            }
//            else {
//                while(true) {
//                    if(!this.adsArr.contains(this.getRandomValue())) {
//                        this.randomValueGenerator();
//                    }
//                    else {
//                        this.setAdsArr(i, this.getRandomValue());
//                        break;
//                    }
//                }
//            }
        }
    }
    
    /**
     * this method will find the total number of ads in the pool or default ads
     * directory
     * ref: http://www.exampledepot.com/egs/java.io/GetFiles.html
     */
    private void analyseAdsPool() {
        File dir;

        String[] children;
        try {
            dir = new File(this.getAdsHomeDirectory());
            children = dir.list();
        } catch (Exception e) {
            System.out.println("Directory not found exception: " + this.getAdsHomeDirectory());
            return;
        }
        
        if (children == null) {
            // Either dir does not exist or is not a directory
            System.out.println("Directory is empty: " + this.getAdsHomeDirectory());
            return;
        } else {
            if(this.adsArr.size() < 1)
                this.setAdsArrSize(children.length);            
            // It is also possible to filter the list of returned files.
            // This example does not return any files that start with '.'.
            FilenameFilter filter = new FilenameFilter() {
                @Override
                public boolean accept(File dir, String name) {
                    if(name.endsWith(".jpeg"))
                        return true;
                    else if(name.endsWith(".jpg"))
                        return true;
                    else if(name.endsWith(".png"))
                        return true;
                    else if(name.endsWith(".bmp"))
                        return true;
                    else
                        return false;
                }
            };
            children = dir.list(filter);
            for (int i = 0; i < children.length; i++) {
                // Get filename of file or directory
                this.addToAdsPool(new Advertisement(i, children[i], this.getAdsHomeDirectory() + "\\" + children[i]));
            }
        }

//        // The list of files can also be retrieved as File objects
//        File[] files = dir.listFiles();
//
//        // This filter only returns directories
//        FileFilter fileFilter = new FileFilter() {
//            @Override
//            public boolean accept(File file) {
//                return file.isDirectory();
//            }
//        };
//        files = dir.listFiles(fileFilter);
    }

    /**
     * Generates a random value using the Random class
     * 
     * generates a random value and sets the randomValue variable
     */
    public void randomValueGenerator() {
        Random rand = new Random();
        int pickedNumber = rand.nextInt(this.getTotalAds());
        this.randomValue = pickedNumber;
    }
    
    /**
     * Gets the random value stored in the private variable
     * 
     * @return randomValue int randomly generated vale
     */
    public int getRandomValue() {
        this.randomValueGenerator();
        return randomValue;
    }

    /**
     * Sets a random value to the private variable
     * 
     * @param randomValue the randomValue to set
     */
    private void setRandomValue(int randomValue) {
        this.randomValue = randomValue;
    }

    /**
     * Gets the total number of ads in the banners direcotry
     * 
     * @return int totalAds total number of ads in the pool
     */
    public int getTotalAds() {
        return totalAds;
    }

    /**
     * Sets the total number of ads in the banners direcotry
     * 
     * @param totalAds int the totalAds to set
     */
    private void setTotalAds(int totalAds) {
        this.totalAds = totalAds;
    }

    /**
     * Gets the home directory of the banners folder
     * 
     * @return String adsHomeDirectory text representation of the ads directory
     */
    public String getAdsHomeDirectory() {
        return adsHomeDirectory;
    }

    /**
     * Sets the home directory of the ads folder
     * 
     * @param adsHomeDirectory the adsHomeDirectory to set
     */
    private void setAdsHomeDirectory(String adsHomeDirectory) {
        this.adsHomeDirectory = adsHomeDirectory;
    }

    /**
     * method for returning entire add pool
     * @return the adsPool
     */
    public ArrayList<Advertisement> getAdsPool() {
        return adsPool;
    }
    
    /**
     * this method returns the string representation of the advertisement
     * list
     * 
     * @return String text representation of the advertisement list
     */
    public String getAdsPoolToString() {
        return this.adsPool.toString();
    }

    /**
     * 
     * @param Advertisement to be added to the pool
     */
    public void addToAdsPool(Advertisement ad) {
        this.adsPool.add(ad);
        this.setTotalAds(this.getTotalAds() + 1);
    }
    
    /**
     * remove an ads from the pool
     * 
     * @param adID id of the add to be removed
     */
    public void removeFromAdsPool(int adID) {
        this.adsPool.remove(adID);
        this.setTotalAds(this.getTotalAds() - 1);
    }

    /**
     * @return the adsArr array list of all the ads
     */
    public ArrayList<Integer> getAdsArr() {
        return adsArr;
    }
    
    /**
     * @return the adsArr get specific ads from the random arraylist of ads
     */
    public int getAdsArr(int pos) {
        return adsArr.get(pos);
    }

    /**
     * 
     * @param pos int position of the ads in the random ads array
     * @param val int value that needs to be set to the position
     */
    private void setAdsArr(int pos, int val) {
//        System.out.println("set pos: "+pos+" val: "+val);
        this.adsArr.add(pos, val);
    }

    /**
     * @param adsArr int current size of the random array list
     */
    private void setAdsArrSize(int size) {
//        System.out.println("size: " + size);
        this.adsArr = new ArrayList<Integer>(size);
        for(int i = 0; i < size; i++) {
            this.adsArr.add(i, 0);
//            System.out.println("pos: "+i+ " val: "+this.adsArr.get(i));
        }
    }
}
